package io.plus.server;

import io.plus.engine.server.Server;
import io.plus.server.store.StoreImpl;

import java.io.IOException;


public class ServerStarter {

    private ServerHandlerImpl serverHandler = null;
    private ServerConfigurationImpl serverConfigImpl = null;

    private Server server = null;
    private UserConfiguration userConfiguration = null;


    public ServerStarter(UserConfiguration userConfiguration) {
        this.userConfiguration = userConfiguration;
        init();
    }

    public static void main(String[] args) throws Exception {
        UserConfiguration userConfiguration = new UserConfiguration("127.0.0.1", 1356);

        ServerStarter serverStarter = new ServerStarter(userConfiguration);

        serverStarter.start();
        //serverStarter.stop();
    }

    public void init() {
        System.setProperty("tio.default.read.buffer.size", String.valueOf(userConfiguration.getReadBufferSize()));
        serverHandler = new ServerHandlerImpl(userConfiguration);


        serverConfigImpl = new ServerConfigurationImpl(userConfiguration, serverHandler);

        if (userConfiguration.getStore() == null) {
            userConfiguration.setStore(new StoreImpl(userConfiguration));
        }

        server = new Server(serverConfigImpl);
    }

    public void start() throws IOException {
        server.start(this.userConfiguration.getBindIp(), this.userConfiguration.getBindPort());
    }

    public void stop() {
        server.stop();
    }
}
